import { requestClient } from '#/api/request';

export namespace UserManagementApi {
  /** 用户列表查询参数 */
  export interface UserListParams {
    username: string;
    status: number;
    current_page: number;
  }

  /** 用户信息接口 */
  export interface UserInfo {
    id: number;
    username: string;
    email?: string;
    status: number;
    createTime: string;
    lastLoginTime?: string;
    role?: string;
  }

  /** 用户列表返回值 */
  export interface UserListResult {
    id: number;
    username: string;
    token_count: string;
    create_time: string;
    status: string;
    user_type: string;
    current_page: number;
    total_page: number;
  }

  /** 创建用户参数 */
  export interface CreateUserParams {
    username: string;
    password: string;
    email?: string;
    role?: string;
  }

  /** 更新用户参数 */
  export interface UpdateUserParams {
    id: number;
    email?: string;
    status?: number;
    role?: string;
  }

  /** 用户设备查询参数 */
  export interface UserDevicesParams {
    username: string;
  }

  /** 用户设备信息 */
  export interface UserDeviceInfo {
    device_id: string;
    device_name: string;
    bind_time: string;
    status: number;
  }

  /** 解除设备绑定的参数 */
  export interface UnbindDeviceParams {
    username: string;
    devices_no: string;
  }

  /** 创建邀请码响应 */
  export interface CreateCodeResponse {
    code: string;
  }

  /** 设置邀请码参数 */
  export interface SetCodeParams {
    username: string;
    invitation_code: string;
  }
}

/**
 * 获取用户列表
 */
export async function getUserListApi(params: UserManagementApi.UserListParams) {
  return requestClient.post<UserManagementApi.UserListResult>(
    '/v1/user/user_list',
    params,
  );
}

/**
 * 获取用户详情
 */
export async function getUserDetailApi(userId: number) {
  return requestClient.get<UserManagementApi.UserInfo>(`/v1/users/${userId}`);
}

/**
 * 创建用户
 */
export async function createUserApi(data: UserManagementApi.CreateUserParams) {
  return requestClient.post('/v1/users', data);
}

/**
 * 更新用户信息
 */
export async function updateUserApi(data: UserManagementApi.UpdateUserParams) {
  return requestClient.put(`/v1/users/${data.id}`, data);
}

/**
 * 删除用户
 * @param userId 用户ID
 */
export async function deleteUserApi(userId: number) {
  return requestClient.post('/v1/user/delete', { user_id: userId });
}

/**
 * 批量删除用户
 */
export async function batchDeleteUsersApi(userIds: number[]) {
  return requestClient.delete('/v1/users/batch', { data: { userIds } });
}

/**
 * 修改用户状态
 */
export async function updateUserStatusApi(userId: number, userStatus: number) {
  return requestClient.post('/v1/user/update_status', {
    user_id: userId,
    user_status: userStatus,
  });
}

/**
 * 重置用户密码
 */
export async function resetUserPasswordApi(userId: number) {
  return requestClient.post(`/v1/users/${userId}/reset-password`);
}

/**
 * 获取用户绑定的设备列表
 * @param params 查询参数
 */
export async function getUserDevicesApi(
  params: UserManagementApi.UserDevicesParams,
) {
  return requestClient.post<UserManagementApi.UserDeviceInfo[]>(
    '/v1/user/devices',
    params,
  );
}

/**
 * 解除设备绑定
 * @param params 参数
 */
export async function unbindDeviceApi(
  params: UserManagementApi.UnbindDeviceParams,
) {
  return requestClient.post('/v1/user/unbind/devices/', params);
}

/**
 * 创建邀请码
 */
export async function createInvitationCodeApi() {
  return requestClient.post<UserManagementApi.CreateCodeResponse>(
    '/v1/wyyl/create_code/',
  );
}

/**
 * 设置邀请码
 */
export async function setInvitationCodeApi(
  params: UserManagementApi.SetCodeParams,
) {
  return requestClient.post('/v1/wyyl/set/code/', params);
}

/**
 * 重置邀请码
 */
export async function resetInvitationCodeApi(username: string) {
  return requestClient.post('/v1/wyyl/reset/code/', { username });
}
